using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Rebel1._1
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        

        Canhao canhao1, canhao2;
        Bola bola;
        bool bolaAtirada;
        SpriteFont fontArial;
        bool vez;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            canhao1 = new Canhao(Content.Load<Texture2D>(@"Texturas\cannon"),
                new Vector2(150, 480),
                0.0f);

            canhao2 = new Canhao(Content.Load<Texture2D>(@"Texturas\cannon"),
                new Vector2(700, 480),
                (float)Math.PI);

            fontArial = Content.Load<SpriteFont>(@"Texturas\Arial");
                
            
        }

        protected override void UnloadContent()
        {
           
        }
        

        protected override void Update(GameTime gameTime)
        {
            KeyboardState teclado = Keyboard.GetState();

            if (canhao1.Vez() == true)
            {

                // Canhao 1
                if (teclado.IsKeyDown(Keys.W))
                {
                    canhao1.rotacao -= 0.01f;
                }
                else if (teclado.IsKeyDown(Keys.S))
                {
                    canhao1.rotacao += 0.01f;
                }
                else if (teclado.IsKeyDown(Keys.A))
                {
                    canhao1.posicao.X--;
                }
                else if (teclado.IsKeyDown(Keys.D))
                {
                    canhao1.posicao.X++;
                }
                if (teclado.IsKeyDown(Keys.Space) && !bolaAtirada)
                {
                    Vector2 posicaoSaida = canhao1.posicao + new Vector2(31.0f - 9, canhao1.textura.Height / 2 - 9);
                    float raioCanhao = canhao1.textura.Width - 31.0f;
                    posicaoSaida += new Vector2((float)Math.Cos(canhao1.rotacao), (float)Math.Sin(canhao1.rotacao)) * raioCanhao;
                    bola = new Bola(Content.Load<Texture2D>(@"Texturas\cannonball"), posicaoSaida, canhao1.rotacao);
                    bolaAtirada = true; ;

                }
            }
            else 
            {

                // Canhao 2
                if (teclado.IsKeyDown(Keys.I))
                {
                    canhao2.rotacao += 0.01f;
                }
                else if (teclado.IsKeyDown(Keys.K))
                {
                    canhao2.rotacao -= 0.01f;
                }
                else if (teclado.IsKeyDown(Keys.J))
                {
                    canhao2.posicao.X--;
                }
                else if (teclado.IsKeyDown(Keys.L))
                {
                    canhao2.posicao.X++;
                } if (teclado.IsKeyDown(Keys.Space) && !bolaAtirada)
                {
                    bola = new Bola(Content.Load<Texture2D>(@"Texturas\cannonball"),
                         new Vector2(canhao2.posicao.X, canhao2.posicao.Y),
                         canhao2.rotacao);

                    bolaAtirada = true; ;
                }
            }
            if (bolaAtirada)
            {
                bola.Update(gameTime);
            }


            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            canhao1.Draw(gameTime, spriteBatch);
            canhao2.Draw(gameTime, spriteBatch);
            if (bolaAtirada)
            {
                bola.Draw(gameTime, spriteBatch);
            }
            spriteBatch.DrawString(fontArial, "Vez do Canhao direito.", new Vector2 (10.0f, 10.0f), Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
